// pages/idleCommunityAdd/idleCommunityAdd.js
import request from "../../utils/request";
import { parse } from "../../utils/utils";
const api = require("../../config/api");
import {
	shareOptions
} from "../../utils/access";
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		imageUrl: "https://image-classify.daieco.com/kejian-quanzi/Og6PvSmPYnibac442aa423ae458cc43a5944de88c034.png", //默认的勾选图片
		imageFlag: true, // 用户是否选择默认 true 默认 false非默认
		communityName: "", // 小区名称
		houseNo: "", //门牌号
		communityDetail: {}, //地址详情
		addFlag: true, //添加还是修改 true 添加 false 修改
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (options.addressCode) {
			// 修改
			this.getAddressDetail(options.addressCode);
			this.setData({
				addFlag: false,
			});
		} else {
			// 添加
			this.setData({
				communityName: parse(options.communityDetail).name,
				communityDetail: parse(options.communityDetail),
				addFlag: true,
			});
		}
	},

	// 设为默认按钮
	selectDefault() {
		this.setData({
			imageFlag: !this.data.imageFlag,
		});
		if (this.data.imageFlag) {
			this.setData({
				imageUrl: "https://image-classify.daieco.com/kejian-quanzi/Og6PvSmPYnibac442aa423ae458cc43a5944de88c034.png",
			});
		} else {
			this.setData({
				imageUrl: "https://image-classify.daieco.com/kejian-quanzi/XIDwkLcBB4rta3b5d888985ea2339c42f2b927134aab.png",
			});
		}
	},

	// 实时获取输入的门牌号
	bindInputValue(e) {
		if (e.detail.value.length <= 30) {
			this.setData({
				houseNo: e.detail.value,
			});
		} else {
			wx.showToast({
				title: `门牌号不能超出30字符~`,
				icon: "none",
			});
			this.setData({
				houseNo: e.detail.value.slice(0, 30),
			});
		}
	},

	// 获取地址详情
	async getAddressDetail(addressCode) {
		const res = await request.get(api.addressDetail, {
			id: addressCode,
		});
		console.log("地址详情", res);
		if (res.httpStatus == 200) {
			this.setData({
				communityName: res?.data.communityName,
				houseNo: res?.data.houseNo,
				communityDetail: res?.data,
				imageUrl:
					res.data.defaultFlag === 0
						? "https://image-classify.daieco.com/kejian-quanzi/XIDwkLcBB4rta3b5d888985ea2339c42f2b927134aab.png"
						: "https://image-classify.daieco.com/kejian-quanzi/Og6PvSmPYnibac442aa423ae458cc43a5944de88c034.png",
				imageFlag: res.data.defaultFlag === 0 ? false : true,
			});
		}
	},

	// 添加地址
	addAddress() {
		let that = this;
		// if (!that.data.houseNo) {
		// 	wx.showToast({
		// 		title: `请输入门牌号`,
		// 		icon: "none",
		// 	});
		// 	return;
		// }
		wx.showModal({
			title: "提示",
			content: "确定保存该地址",
			success(res) {
				if (res.confirm) {
					that.addAddressSure();
				} else if (res.cancel) {
					console.log("用户点击取消保存");
				}
			},
		});
	},
	// 确认添加或修改地址
	async addAddressSure() {
		if (!this.data.addFlag) {
			let params = {
				communityCode: this.data.communityDetail.communityCode,
				communityName: this.data.communityDetail.communityName,
				addressDetail: this.data.communityDetail.addressDetail,
				// houseNo: this.data.houseNo,
				location: this.data.communityDetail.lon,
				defaultFlag: this.data.imageFlag ? 1 : 0,
				provinceCode: this.data.communityDetail.provinceCode,
				provinceName: this.data.communityDetail.provinceName,
				cityCode: this.data.communityDetail.cityCode,
				cityName: this.data.communityDetail.cityName,
				areaCode: this.data.communityDetail.areaCode,
				areaName: this.data.communityDetail.areaName,
				id: this.data.communityDetail.id,
			};
			// 修改
			const res = await request.post(api.addressUpdate, params);
			console.log("修改地址-res", res);
			if (res.httpStatus == 200) {
				wx.showToast({
					title: `修改成功`,
					icon: "none",
				});
				setTimeout(() => {
					wx.navigateBack({ url: "/pages/idleCommunity/idleCommunity" });
				}, 300);
			}
		} else {
			// 添加
			let params = {
				communityCode: this.data.communityDetail.id,
				communityName: this.data.communityDetail.name,
				addressDetail: this.data.communityDetail.address,
				houseNo: this.data.houseNo,
				location: this.data.communityDetail.location,
				defaultFlag: this.data.imageFlag ? 1 : 0,
				provinceCode: this.data.communityDetail.pcode,
				provinceName: this.data.communityDetail.pname,
				cityCode: this.data.communityDetail.citycode,
				cityName: this.data.communityDetail.cityname,
				areaCode: this.data.communityDetail.adcode,
				areaName: this.data.communityDetail.adname,
			};
			console.log("添加地址", params);
			const res = await request.post(api.addressAdd, params);
			console.log("添加地址-res", res);
			if (res.httpStatus == 200) {
				wx.showToast({
					title: `添加成功`,
					icon: "none",
				});
				setTimeout(() => {
					wx.navigateBack({ delta: 2 });
				}, 300);
			}
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
});
